package cn.sailing.plugins.position.baidu;

import android.content.Context;
import android.util.Log;

import com.baidu.trace.LBSTraceClient;
import com.baidu.trace.Trace;
import com.baidu.trace.model.OnTraceListener;
import com.baidu.trace.model.PushMessage;

import java.util.Objects;

import cn.sailing.plugins.position.ITrackListener;
import cn.sailing.plugins.position.Utils;

public class BaiduTrackService {
    private Context context;
    private long SERVICE_ID = 241016;
    private String TERMINAL_NAME = Utils.GetDeviceName();
    private Trace trace;
    private LBSTraceClient client;

    private OnTraceListener listener = new OnTraceListener() {
        @Override
        public void onBindServiceCallback(int i, String s) {

        }

        @Override
        public void onStartTraceCallback(int i, String s) {
            client.startGather(this);
        }

        @Override
        public void onStopTraceCallback(int i, String s) {

        }

        @Override
        public void onStartGatherCallback(int i, String s) {

        }

        @Override
        public void onStopGatherCallback(int i, String s) {

        }

        @Override
        public void onPushCallback(byte b, PushMessage pushMessage) {
            Log.i("onPushCallback", pushMessage.getFenceAlarmPushInfo().getCurrentPoint().getLocation().toString());
        }

        @Override
        public void onInitBOSCallback(int i, String s) {

        }

        @Override
        public void onTraceDataUploadCallBack(int i, String s, int i1, int i2) {

        }
    };

    public BaiduTrackService(Context ctx, ITrackListener listener) {
        context = ctx;
        LBSTraceClient.setAgreePrivacy(ctx,true);
        try {
            client = new LBSTraceClient(context);
            client.setInterval(5, 10);
        } catch (Exception e) {
            Log.e("初始化baidu鹰眼服务", Objects.requireNonNull(e.getMessage()));
        }
        trace = new Trace(SERVICE_ID, TERMINAL_NAME, false);
    }

    public void Start() {
        client.startTrace(trace, listener);
    }

    public void Stop() {
        client.stopTrace(trace, listener);
    }
}
